package org.example.mapstruct.mapper;

import org.example.mapstruct.common.dto.CarDto;
import org.example.mapstruct.common.entity.Car;
import org.example.mapstruct.common.util.DateMapper;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Set;
import java.util.stream.Stream;


@Mapper(uses = DateMapper.class)
public interface CarMapper {

    CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);

    /**
     * 默认会匹配到多个 car -> carDto 的转换方法，需要指定对应的转换方法
     *
     * @param cars
     * @return
     */
    @IterableMapping(qualifiedByName = "useDateMapping")
    List<CarDto> carsToCarDtos(List<Car> cars);

    @IterableMapping(qualifiedByName = "useDateMapping")
    Set<CarDto> carsToCarDtos(Set<Car> cars);

    @IterableMapping(qualifiedByName = "useDateMapping")
    Set<CarDto> carListToCarDtoSet(List<Car> cars);

    /**
     * 将 Car 转换成 CarDto
     *
     * @param car Car 实例
     * @return 对应的 DTO
     */
    @Named("defaultMapping")
    @Mapping(source = "numberOfSeats", target = "seatCount")
    @Mapping(target = "manufacturingDate", ignore = true)
    CarDto carToCarDto(Car car);

    /**
     * 将 Car 转换成 CarDto，不忽略 Date
     *
     * @param car Car 实例
     * @return 对应的 DTO
     */
    @Named("useDateMapping")
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto carToCarDtoUseDate(Car car);


    List<CarDto> carsToCarDtos(Stream<Car> cars);

}
